package com.zjs.mybatis.contorller;

import com.zjs.mybatis.pojo.User;
import com.zjs.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserContorller {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/getUser/{userName}")
	public User getUser(@PathVariable(value = "userName") String userName) {
		User user = userService.selectUserByName(userName);
		if(user != null) {
			return user;
		}else {
			return null;
		}
	}
	
	@GetMapping(value = "/getString")
	public String print() {
		return "Hello Server_DB";
	}
	
	@GetMapping(value = "/getUserById/{uid}")
	public User getUser2(@PathVariable(value = "uid") int uid) {
		return userService.selectById(uid);	
	}
	
	@GetMapping(value = "/deleteUserId/{uid}")
	public int deleteUserById(@PathVariable(value = "uid") int uid) {
	
		return  userService.deleteUser(uid);
	}
	@GetMapping(value = "/getAllUser")
	public List<User> getAllUser(){
		List<User> list = userService.selectAllUser();
		return list;
	}
	
	@PostMapping(value = "/insertUser")
	public int insertUser(@RequestBody User user){
		return userService.insertUser(user);
		
	}
	@PostMapping("updateUser")
	public int updateUser(@RequestBody User user){
		return  userService.updateUser(user);
		
	}
	
	@RequestMapping(value = "/addUser")
	public int addUser() {
		List<User> list = new ArrayList<User>();
		list.add(new User("py2","py"));
		list.add(new User("sty2","sty"));
		list.add(new User("wss2","wss"));
		list.add(new User("mss2","mss"));
		
		list.add(new User("rnn2","rnn"));
		list.add(new User("wss12","wss1"));
		list.add(new User("lyc2","lyc"));
		
		list.add(new User("zyt2","zyt"));
		list.add(new User("ccm2","ccm"));
		list.add(new User("lwm2","lwm"));
		
		
		return userService.addUser2(list);
	}
	
	//批量更新有问题
	@GetMapping(value = "/updateUserTwo/{flag}")
	public int updateUserTwo(@PathVariable(value = "flag") int flag) {
		List<User> list = new ArrayList<User>();
		list.add(new User("zyy","1997",flag));
		
		return userService.updateUserTwo(list);
	}
	@GetMapping(value = "/test")
	public String test(){
		return "hello  1024 ";
	}
}
